package com.ihy.controller;

import com.ihy.entity.About;
import com.ihy.entity.Clause;
import com.ihy.service.ServiceAbout;
import com.ihy.service.ServiceClause;
import com.ihy.utils.RedisTemplateUtil;
import com.ihy.utils.ServiceStatusUtil;
import com.ihy.vo.ResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@Slf4j
public class OperationController {
    @Autowired
    private ResultResponse resultResponse;
    @Autowired
    private RedisTemplateUtil redisTemplateUtil;
    @Autowired
    private ServiceClause serviceClause;
    @Autowired
    private ServiceAbout serviceAbout;

    //跳转到登录
    @RequestMapping("login")
    public ResultResponse signIn() {
        resultResponse.setCode(407);
        resultResponse.setMessage("欢迎登录");
        resultResponse.setData("未登录将会跳转过来");
        return resultResponse;
    }

    /**
     * 注销功能
     *
     * @param userNameToken 用户名 + 设备 + Token
     * @return 状态信息
     */
    @RequestMapping("logout")
    public ResultResponse logout(String userNameToken) {
        //redisTemplateUtils.del("imgSwipeSuccess");
        redisTemplateUtil.del("qqState");
        redisTemplateUtil.del(userNameToken);
        //redisTemplateUtils.del("XPosition");
        resultResponse.setCode(200);
        resultResponse.setMessage("注销完成");
        resultResponse.setData("注销会清空已验证信息");
        return resultResponse;
    }

    /**
     * 服务
     *
     * @param ip      ip地址
     * @param port    端口
     * @param timeout 超时时间
     * @return true or false
     */
    @RequestMapping("service")
    public Boolean serviceStatus(String ip, String port, Integer timeout) {
        return ServiceStatusUtil.isReachable(ip, port, timeout);
    }

    /**
     * 条款信息
     *
     * @param id 标识
     * @return 信息
     */
    @RequestMapping("clause")
    public ResultResponse clause(Integer id) {
        try {
            Clause clause = serviceClause.getClause(id);
            resultResponse.setCode(200);
            resultResponse.setData(clause);
            resultResponse.setMessage("请求成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultResponse.setCode(500);
            resultResponse.setData(null);
            resultResponse.setMessage("请求失败");
        }
        return resultResponse;
    }

    /**
     * 关于信息
     *
     * @param id 标识
     * @return 信息
     */
    @RequestMapping("about")
    public ResultResponse about(Integer id) {
        try {
            About about = serviceAbout.getAbout(id);
            resultResponse.setCode(200);
            resultResponse.setData(about);
            resultResponse.setMessage("请求成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultResponse.setCode(500);
            resultResponse.setData(null);
            resultResponse.setMessage("请求失败");
        }
        return resultResponse;
    }
}